const router = require('koa-router')()
const sqlQuery = require('../db')
router.prefix('/classify')

router.get('/findAllClass', async (ctx, next) => {
  try {
    let sql = `SELECT * FROM happy_class WHERE state=1`;
    let result = await sqlQuery(sql, []);
    await Promise.all(result.map(async item => {
      item.children = await sqlQuery('SELECT * FROM happy_class WHERE state=1 AND pid', [item.id])
    }));
    ctx.body = {
      status: 200,
      message: result
    }
  } catch (error) {
    next(error)
  }
})

router.get('/icon', async (ctx, next) => {
  try {
    let sql = "select * from icon where i_start=1  ";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})
router.get('/icons', async (ctx, next) => {
  try {
    let sql = "select * from icon where i_start=1 order by i_id desc limit 1,4 ";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})

//左侧菜单数据
router.get("/leftmenu", async (ctx, next) => {
  try {

    let sql = "select * from leftmenu where did=1";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 1,
      message: result
    }

  } catch (error) {
    next(error);
  }
});
//查询右侧内容
router.get("/rightcon", async (ctx, next) => {
  try {

    let sql = "select * from rightcontent where pid=1";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 1,
      message: result
    }

  } catch (error) {
    next(error);
  }
});
//右侧内容数据
router.get("/rightcontent/:id", async (ctx, next) => {
  try {
    let { id } = ctx.params;
    let sql = "select r.name,r.img_url,r.id from leftmenu l,rightcontent r where l.id=? and l.id=r.pid";
    let result = await sqlQuery(sql, [id]);
    ctx.body = {
      status: 1,
      message: result
    }

  } catch (error) {
    next(error);
  }
});
//内容
router.get("/content/:i", async (ctx, next) => {
  try {
    let { i } = ctx.params
    let sql = "select c.id, c.url from happy_video as c inner join rightcontent as r on r.id=c.class_id where r.id=?";
    let result = await sqlQuery(sql, [i]);
    ctx.body = {
      status: 1,
      message: result
    }

  } catch (error) {
    next(error);
  }
});
//查询右侧所有内容
router.get("/rco", async (ctx, next) => {
  try {

    let sql = "select * from rightcontent";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 1,
      message: result
    }

  } catch (error) {
    next(error);
  }
});
module.exports = router